/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package genomemap.cef.command;

import cef.command.Command;
import cef.command.CommandException;
import genomemap.cef.event.CreateRepeatsMap_Evt;
import genomemap.data.OrganismData;
import genomemap.data.OrganismDataUtil;
import genomemap.map.ProbeMap;
import genomemap.map.RepeatsDistMapPanel;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Susanta Tewari
 * @since Sep 26, 2011
 */
public class RepeatsMap_Cmd extends Command<CreateRepeatsMap_Evt> {

    private RepeatsDistMapPanel repeatsDistMap;
    private Map<Integer, RepeatCountVO> repeat_counts;

    public RepeatsMap_Cmd(final CreateRepeatsMap_Evt event) {
        super(event);
    }

    @Override
    public void executeImpl() throws CommandException {

        final OrganismData data                 = getEvent().getOrganismData();
        final List<ProbeMap> probeMaps          = OrganismDataUtil.probeMaps(data);
        final Map<String, Integer> cloneRepeats = OrganismDataUtil.repeats(data);

        repeatsDistMap = new RepeatsDistMapPanel("data-1-2-repeats-distn-map", probeMaps,
                data.getCloneLength(), cloneRepeats);

        final Map<Integer, int[]> map_1   = OrganismDataUtil.countRepeats(data);
        final Map<Integer, int[][]> map_2 = OrganismDataUtil.countRepeatsTransitions(data);

        repeat_counts = new HashMap<>(map_1.size());

        for (final Integer lg : map_1.keySet()) {
            repeat_counts.put(lg, new RepeatCountVO(map_1.get(lg), map_2.get(lg)));
        }
    }

    /**
     * @return key: linkage group; value: repeat counts
     */
    public Map<Integer, RepeatCountVO> getRepeatCounts() {

        checkCmdFinished();

        return repeat_counts;
    }

    /**
     * @return repeats distribution map
     */
    public JPanel getRepeatsDistnPanel() {

        checkCmdFinished();

        return repeatsDistMap;
    }

    /**
     * @version 1.3.0
     * @since 1.3.0
     * @history Created on 02/19/2013
     * @author Susanta Tewari
     */
    public static class RepeatCountVO {

        private final int[] counts;
        private final int[][] transition_counts;

        public RepeatCountVO(final int[] counts, final int[][] transition_counts) {

            this.counts            = counts;
            this.transition_counts = transition_counts;
        }

        public int[] getCounts() {
            return counts;
        }

        public int[][] getTransitionCounts() {
            return transition_counts;
        }
    }
}
